// pages/manage/manage.ts
import {
  addSiteIntofoodList,
  getFoodClass,
  getFoodList,
  permissionToCanteenName,
  permissionToCanteenId
} from "../../apis/apis"
var app = getApp();
var apiUrl = getApp().globalData.apiUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    canteenName: "饭堂",
    sideIndex: 0,
    canteenId: 0,
    classArr: [],
    foodArr: [],
    foodPageNo: 1,
    canteenArr:["一饭一楼","一饭三楼","二饭","三饭"],
    pickerIndex:0,
    disPicker:true,
    activeKey: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setNavigationBarTitle({
      title: '悦食生活-管理端',
    })
    if (app.globalData.userInfo.permission==2) {
      this.setData({
        disPicker:false,
        canteenName:this.data.canteenArr[0],
        canteenId:1
      })
    }
    else{
      this.setData({
      canteenName:permissionToCanteenName(app.globalData.userInfo.permission,app.globalData.canteenList),
      canteenId:permissionToCanteenId(app.globalData.userInfo.permission,app.globalData.canteenList),
      pickerIndex:app.globalData.userInfo.permission - 3
    })
  }
  },

  onChange(options) {
    this.setData({
      sideIndex: options.detail,
      activeKey:options.detail,
    })
    getFoodList(this.data.foodPageNo,6,this.data.canteenId,this.data.classArr[options.detail].id).then(res=>{
      addSiteIntofoodList(this.data.canteenName, res.rows)
      this.setData({
        foodArr: res.rows
      })
    })
  },
  
  pickerChange(e){
    this.setData({
      pickerIndex:e.detail.value,
      canteenName:this.data.canteenArr[this.data.pickerIndex],
      canteenId:parseInt(e.detail.value)+1
    })
    this.onShow();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    getFoodClass(this.data.canteenId).then(res => {
      this.setData({
        classArr: res.reverse()
      });
      if (this.data.classArr[0]) {
        getFoodList(this.data.foodPageNo, 6, this.data.canteenId, this.data.classArr[0].id).then(res => {
          addSiteIntofoodList(this.data.canteenName, res.rows)
          this.setData({
            foodArr: res.rows
          })
        })
      }
    })
  },
  goManageFoodClassify() {
    wx.navigateTo({
      url: 'manageFoodClassify?canteenId='+this.data.canteenId,
    })
  },
  goManageFoodList() {
    wx.navigateTo({
      url: 'manageFoodList?canteenId='+this.data.canteenId,
    })
  },
  goManageComment(){
    wx.navigateTo({
      url: 'manageComment?canteenId='+this.data.canteenId,
    })
  },
  continueLoad(){
    this.setData({
      foodPageNo:this.data.foodPageNo+1
    })
    console.log(this.data.activeKey);
    console.log(this.data.classArr[this.data.activeKey]);
    getFoodList(this.data.foodPageNo, 6, this.data.canteenId, this.data.classArr[this.data.activeKey].id).then(res => {
      addSiteIntofoodList(this.data.canteenName, res.rows)
      let oldArr=this.data.foodArr
      let newArr=oldArr.concat(res.rows)
      this.setData({
        foodArr: newArr
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})